package net.intellect.collectpro.main;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import net.intellect.collectpro.util.Util;

import org.apache.log4j.Logger;

/**
 * Llama al proceso de procesar pagos
 * @author Intellect International(GRE)
 *
 * Last change: Modificar la logica para poder manejar pagos multiempresa, o pagos para una sola
 * empresa
 * @author Intellect International (TRF)
 */
public class Pago {

	protected static Logger logger = Logger.getLogger("pagos");


	/**
	 * Is executed when the payments jar is used. 
	 * @param args[0] Company id ("todos" or "Todos" when all payments should be processed, 
	 * or the company Id for which payments must be processed)
	 * @param args[1] Salesforce valid user name
	 * @param args[2] Salesforce valid password for the user defined
	 * @param args[3] Boolean flag to let us know if there is a security token or not
	 * @param args[4] Salesforce valid security token corresponded to the user defined
	 * @param args[5] Boolean flag to let us know if we should use Sandbox environment or production
	 */
	public static void main(String[] args) {
	
		try {
	 		       
			// Validates all arguments were included
			if (args.length<7) {
				logger.error("ARGUMENTOS INVALIDOS.(EMPRESA, USER, PASSWORD, TRUE|FALSE, TESTIGO SEGURIDAD,URL PROXY,PUERTO PROXY)");

			} else {
				// Reads arguments
				String company = args[0];
				String user = args[1];
				String password = args[2];
				String token = (args[3].equalsIgnoreCase("true"))? args[4]: null;
				String urlProxy = null;
				String puertoProxy = null;
				boolean isSandBox = false;
								
				if (!args[5].toLowerCase().equals("null")) {
					urlProxy = args[5];
					puertoProxy = args[6];
				}					
				
				if (args.length>7) {
					isSandBox = Boolean.parseBoolean(args[7]);
				}				
				Date date = new Date(); // Takes system date
				/*
				Calendar miCal = Calendar.getInstance();				
				miCal.set(2009, 09, 7);				
				System.out.print(miCal.getTime());
				date = miCal.getTime();
				*/
				
				System.out.println("Procesar pagos.");
				net.intellect.collectpro.gestores.Pago gPagos = 
					new net.intellect.collectpro.gestores.Pago(
							user, password, token, company, isSandBox,urlProxy,puertoProxy);
				System.out.println("Procesar pagos." +
						"\nHora inicio: "  + Util.parsearDateTimeString(new Date()));
			    
				gPagos.procesarPagosDia(date);

				System.out.println("Hora fin: "  + Util.parsearDateTimeString(new Date()));

			}

			/**
				Date fechaPagos = new Date();				
				
				Calendar miCal = Calendar.getInstance();				
				miCal.set(2009, 09, 7);				
				System.out.print(miCal.getTime());
				fechaPagos = miCal.getTime();
								
			}*/
		}catch (Exception e) {
			logger.error(e);
		}

	}

}
